package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.utils.Utils;

public class SelectListActivity extends BaseAppCompatActivity
{

    private static final String TAG = SelectListActivity.class.getSimpleName();
    private ListView selectlist_lv;
    private String[] strs = new String[]{};
    private TextView    textView;
    private ImageView   imageView;
    private String      equalStr;
    private BaseAdapter baseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectlist);

        Utils.initTitle("请选择", this, true);

        Bundle b = this.getIntent().getExtras();
        strs = b.getStringArray("ls");
        equalStr = b.getString("equalStr");
        selectlist_lv = (ListView) findViewById(R.id.selectlist_lv);
        selectlist_lv = (ListView) findViewById(R.id.selectlist_lv);//得到ListView对象的引用 /*为ListView设置Adapter来绑定数据*/
        baseAdapter = new BaseAdapter()
        {
            @Override
            public int getCount()
            {
                return strs.length;
            }

            @Override
            public Object getItem(int i)
            {
                return i;
            }

            @Override
            public long getItemId(int i)
            {
                return i;
            }

            @Override
            public View getView(final int i, View view, final ViewGroup viewGroup)
            {
                if (view == null)
                {
                    view = View.inflate(SelectListActivity.this, R.layout.select_listitem, null);
                    textView = (TextView) view.findViewById(R.id.text1);
                    imageView = (ImageView) view.findViewById(R.id.select_listitem_image);
                }
                textView.setText(strs[i]);
                if (strs[i].equals(equalStr))
                {
                    imageView.setVisibility(View.VISIBLE);
                }
                view.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        for (int i = 0; i < viewGroup.getChildCount(); i++)
                        {
                            viewGroup.getChildAt(i).findViewById(R.id.select_listitem_image).setVisibility(View.GONE);
                        }
                        imageView = (ImageView) view.findViewById(R.id.select_listitem_image);
                        imageView.setVisibility(View.VISIBLE);
                        Intent intent = new Intent();
                        intent.putExtra("itemName", strs[i]);
                        SelectListActivity.this.setResult(RESULT_OK, intent);
                        finish();
                    }
                });
                return view;
            }
        };
        selectlist_lv.setAdapter(baseAdapter);
    }
}

